// LSL script generated: RLV-New.Addons.DV_MAIN_Tint.lslp Sun May  2 00:09:51 Westeuropäische Sommerzeit 2010
list gMenu = [];
key ToucherID;
integer channel_dialog;
integer listen_id;
string msg;
integer gPage;
//

list pageMenu(list in,integer page){
    list out;
    integer maxpages = (llGetListLength(in) / 9);
    (page = (page % (maxpages + 1)));
    (out = ["<<","Main",">>"]);
    integer _listcount = ((page * 9) + 9);
    integer _i = (_listcount - 9);
    for (_i; (_i < _listcount); (_i++)) {
        if (((_i <= llGetListLength(in)) && (llList2String(in,_i) != ""))) (out += llList2String(in,_i));
    }
    return out;
}

makeMenu(){
    integer n;
    integer linkcount = llGetNumberOfPrims();
    (gMenu = []);
    for ((n = 2); (n <= linkcount); (n++)) {
        string desc = ((string)llGetObjectDetails(llGetLinkKey(n),[1]));
        (gMenu += desc);
    }
}



displayMenu(){
    list tempMain = [];
    (msg = "Pick one");
    if ((llGetListLength(gMenu) < 10)) {
        (tempMain = ([" ","Back"," "] + tempMain));
    }
    else  {
        (tempMain += gMenu);
        (tempMain = pageMenu(tempMain,gPage));
    }
    (channel_dialog = (-1 * ((integer)("0x" + llGetSubString(((string)llGetKey()),-5,-1)))));
    (listen_id = llListen(channel_dialog,"",NULL_KEY,""));
    llDialog(ToucherID,msg,tempMain,channel_dialog);
    llSetTimerEvent(45.0);
}

default {

    state_entry() {
        makeMenu();
    }

     listen(integer channel,string name,key id,string message) {
        if ((channel == channel_dialog)) {
            if ((message == "Back")) {
                llMessageLinked(-4,1337,"DV_MAIN_MISC",ToucherID);
            }
            if ((message == "<<")) {
                (gPage--);
                integer maxpages = (llGetListLength(gMenu) / 9);
                if ((gPage < 0)) {
                    (gPage = maxpages);
                }
                displayMenu();
            }
            else  if ((message == ">>")) {
                (gPage++);
                displayMenu();
            }
        }
    }

    
    link_message(integer sender,integer num,string str,key id) {
        if ((num == 1337)) {
            if ((str == llGetScriptName())) {
                llMessageLinked(-4,1338,llGetScriptName(),NULL_KEY);
                (ToucherID = id);
                displayMenu();
            }
        }
        if ((num == 1338)) {
            if ((str != llGetScriptName())) {
                llListenRemove(listen_id);
            }
        }
    }

  	
  	on_rez(integer num) {
        makeMenu();
    }

  	

  	
  	timer() {
        llSetTimerEvent(0.0);
        llListenRemove(listen_id);
    }
}
